package com.cictec.middleware.tde.rm.protocol.rm.decode;

import java.nio.ByteBuffer;

import org.apache.mina.core.session.IoSession;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.tde.exception.MessageDecoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x0ATerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 
 * 福田电动车指令解码
 * 
 * @author yy
 *
 */
@TerminalMessageDecoder(0x0A)
public class Rm0x0ADecoder extends AbstractMessageDecoder {

	@Override
	public TerminalMessage decode(Header header, ByteBuffer in, IoSession session) throws MessageDecoderException {
		// TODO Auto-generated method stub

		Rm0x0ATerminalMessage msg = new Rm0x0ATerminalMessage(header);

		msg.setContractNumber(BinaryUtils.unsignedByteToInt(in));
		msg.setLat(RmBinaryUtils.dwordToLatLngRM(in));
		msg.setLgt(RmBinaryUtils.dwordToLatLngRM(in));
		msg.setSpeedNO(BinaryUtils.unsignedByteToInt(in));
		msg.setDirectionngle(BinaryUtils.unsignedByteToInt(in) / 2);
		msg.setTheAltitude(BinaryUtils.wordToInt(in));
		in.get(new byte[4]);
		msg.setCanLength(BinaryUtils.unsignedByteToInt(in));
		if (msg.getCanLength() == 36) {

			msg.setId2(BinaryUtils.dwordToLong(in));
			if (msg.getId2() == 0x18FFA017) {
				byte[] bytes = new byte[3];
				in.get(bytes);
				msg.setMileage(BinaryUtils.threeBytesToIntHigh(bytes) / 8);
				byte[] byteWord = new byte[2];
				in.get(byteWord);
				msg.setSpeed(BinaryUtils.wordToIntHigh(byteWord) / 10);
				byte[] bytesRe = new byte[3];
				in.get(bytesRe);
				msg.setReserved3(BinaryUtils.threeBytesToIntHigh(bytesRe));// 预留3字节
			} else {
				BinaryUtils.bytesToString(in, 8);
			}
			msg.setId1(BinaryUtils.dwordToLong(in));
			if (msg.getId1() == 0x18FFA117) {
				msg.setPressure1(BinaryUtils.unsignedByteToInt(in) * 10);
				msg.setPressure2(BinaryUtils.unsignedByteToInt(in) * 10);

				msg.setReserved1(BinaryUtils.wordToIntHigh(in));// 预留2字节

				byte[] bytes = new byte[1];
				in.get(bytes);
				msg.setChargingHatchState(BinaryUtils.byteBitToInt(bytes[0], 8));
				msg.setTheRearState(BinaryUtils.byteBitToInt(bytes[0], 7));
				msg.setReservedBit1(BinaryUtils.byteBitToInt(bytes[0], 6));
				msg.setReservedBit2(BinaryUtils.byteBitToInt(bytes[0], 5));
				msg.setReservedBit3(BinaryUtils.byteBitToInt(bytes[0], 4));
				msg.setReverseSignals(BinaryUtils.byteBitToInt(bytes[0], 3));
				msg.setHandBrakeSignal(BinaryUtils.byteBitToInt(bytes[0], 2));
				msg.setTheKeyBand(BinaryUtils.byteBitToInt(bytes[0], 1));
				byte[] bytes6 = new byte[1];
				in.get(bytes6);

				msg.setReservedBit4(BinaryUtils.byteBitToInt(bytes6[0], 8));
				msg.setReservedBit5(BinaryUtils.byteBitToInt(bytes6[0], 7));
				msg.setReservedBit6(BinaryUtils.byteBitToInt(bytes6[0], 6));
				msg.setTheFrontDoor(BinaryUtils.byteBitToInt(bytes6[0], 5));
				msg.setInTheDoor(BinaryUtils.byteBitToInt(bytes6[0], 4));
				msg.setInstrumentSystem(BinaryUtils.byteBitToInt(bytes6[0], 3));
				msg.setVoltage(BinaryUtils.byteBitToInt(bytes6[0], 2));
				msg.setTheBackDoor(BinaryUtils.byteBitToInt(bytes6[0], 1));
				msg.setReserved2(BinaryUtils.wordToIntHigh(in));
			} else {
				BinaryUtils.bytesToString(in, 8);
			}

			msg.setId3(BinaryUtils.dwordToLong(in));
			if (msg.getId2() == 0x18FFA217) {
				byte[] bytes = new byte[1];

				in.get(bytes);
				msg.setLeftTurnSignal(BinaryUtils.bytesBitToInt(bytes[0], 8, 7));
				msg.setBeforeFogLamps(BinaryUtils.bytesBitToInt(bytes[0], 6, 5));
				msg.setRightTurnSignal(BinaryUtils.bytesBitToInt(bytes[0], 4, 3));
				msg.setAfterFogLamps(BinaryUtils.bytesBitToInt(bytes[0], 2, 1));
				byte[] bytes2 = new byte[1];
				in.get(bytes2);
				msg.setBrakeLights(BinaryUtils.bytesBitToInt(bytes2[0], 8, 7));
				msg.setBrakeAlarmSignal(BinaryUtils.bytesBitToInt(bytes2[0], 6, 5));
				msg.setPressure1Alarm(BinaryUtils.bytesBitToInt(bytes2[0], 4, 3));
				msg.setBrakingPressure(BinaryUtils.bytesBitToInt(bytes2[0], 2, 1));
				byte[] bytes3 = new byte[1];
				in.get(bytes3);
				msg.setPressure2Alarm(BinaryUtils.bytesBitToInt(bytes3[0], 8, 7));
				msg.setElectricHorn(BinaryUtils.bytesBitToInt(bytes3[0], 6, 5));
				msg.setSuperLightSignal(BinaryUtils.bytesBitToInt(bytes3[0], 4, 3));
				msg.setHighBeam(BinaryUtils.bytesBitToInt(bytes3[0], 2, 1));
				byte[] bytes4 = new byte[1];
				in.get(bytes4);
				msg.setDippedHeadlight(BinaryUtils.bytesBitToInt(bytes4[0], 8, 7));
				msg.setSmallLights(BinaryUtils.bytesBitToInt(bytes4[0], 6, 5));
				msg.setDangerSignals(BinaryUtils.bytesBitToInt(bytes4[0], 4, 3));
				msg.setEmergencyValve(BinaryUtils.bytesBitToInt(bytes4[0], 2, 1));
				byte[] bytes5 = new byte[1];
				in.get(bytes5);
				msg.setKeysONFile(BinaryUtils.bytesBitToInt(bytes5[0], 8, 7));
				msg.setKeysACCFile(BinaryUtils.bytesBitToInt(bytes5[0], 6, 5));
				msg.setABS(BinaryUtils.bytesBitToInt(bytes5[0], 4, 3));
				msg.setASR(BinaryUtils.bytesBitToInt(bytes5[0], 2, 1));
				byte[] bytes6 = new byte[1];
				in.get(bytes6);
				msg.setDefrost(BinaryUtils.bytesBitToInt(bytes6[0], 8, 7));
				msg.setAirConditioningPower(BinaryUtils.bytesBitToInt(bytes6[0], 6, 5));
				msg.setWaterLevel(BinaryUtils.bytesBitToInt(bytes6[0], 4, 3));
				msg.setReservedBit7(BinaryUtils.byteBitToInt(bytes6[0], 2));
				msg.setReservedBit8(BinaryUtils.byteBitToInt(bytes6[0], 1));

				msg.setReserved4(BinaryUtils.wordToInt(in));// 预留2字节
			} else {
				BinaryUtils.bytesToString(in, 8);
			}

		} else {
			BinaryUtils.bytesToString(in, msg.getCanLength());
		}

		msg.setReserved(BinaryUtils.wordToInt(in));

		return msg;
	}

}
